package com.yunquan.ohana.ui.calendar.task

import androidx.core.graphics.toColorInt
import androidx.databinding.ObservableField
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.RoleEntity
import com.yunquan.ohana.entity.TasksToRoleVo
import com.yunquan.ohana.utils.Utils

class TaskRoleItemViewModel(viewModel: TaskViewModel) : ItemViewModel<TaskViewModel>(viewModel) {

    var data: RoleEntity? = null

    val rolePrefix = ObservableField<String>()
    val outer = ObservableField<Int>()
    val contentColor = ObservableField<Int>()
    val centerColor = ObservableField<Int>()
    val checked = ObservableField(false)

    constructor(viewModel: TaskViewModel, data: RoleEntity) : this(viewModel) {
        this.data = data
        this.rolePrefix.set(data.name?.first()?.toString())
        this.outer.set(data.getColorData().normal.toColorInt())
        contentColor.set(Utils.instance.context.getColor(R.color.c_20))
        centerColor.set(data.getColorData().back.toColorInt())
    }

    fun setChecked(b: Boolean) {
        checked.set(b)
        if (b) {
            centerColor.set(data!!.getColorData().inner.toColorInt())
            contentColor.set(Utils.instance.context.getColor(R.color.white))
        } else {
            centerColor.set(data!!.getColorData().back.toColorInt())
            contentColor.set(Utils.instance.context.getColor(R.color.c_20))
        }
    }

    val onCheckChange = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val b = checked.get() ?: false
            if (!b) {
                viewModel.onChangeRole(data!!)
            } else {
                viewModel.onChangeRole(null)
            }
        }
    })
}